#pragma once
#include "Protocol.hpp"

class Calculator
{
public:
    Calculator() {}
    ~Calculator() {}
    Response Excute(const Request &req)
    {
        Response res;
        switch (req.Oper())
        {
        case '+':
            res.SetResult(req.X() + req.Y());
            res.SetCode(0);
            break;
        case '-':
            res.SetResult(req.X() - req.Y());
            res.SetCode(0);
            break;
        case '*':
            res.SetResult(req.X() * req.Y());
            res.SetCode(0);
            break;
        case '/':
            if (req.Y() == 0)
            {
                res.SetCode(1);
            }
            else
            {
                res.SetResult(req.X() / req.Y());
                res.SetCode(0);
            }

            break;
        case '%':
            if (req.Y() == 0)
            {
                res.SetCode(2);
            }
            else
            {
                res.SetResult(req.X() / req.Y());
                res.SetCode(0);
            }
            break;
        default:
            res.SetCode(3);
            break;
        }
        return res;
    }
}cal;


std::string HandlerRequest(std::string& inbuffer)
{
    std::string content;
    std::string return_str;
    while(Decode(inbuffer,&content))
    {
        //此时是一个完整的报文
        //1. 反序列化Request
        Request req;
        req.deserialization(content);
        //构建应答
        Response rep = cal.Excute(req);
        rep.Print();
        std::string result_str;
        //进行序列化
        rep.serialization(&result_str);
        //封装报头
        Encode(result_str);
        //添加进入返回字符串
        return_str += result_str;
    }
    return return_str;
}